﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Common;
using Model;
using Server;

namespace BearingOiling
{
    /// <summary>
    /// RecordView.xaml 的交互逻辑
    /// </summary>
    public partial class RecordView : Window
    {
        private RecordView()
        {
            InitializeComponent();
            this.Closed += RecordView_Closed;
            this.Loaded += RecordView_Loaded;
        }

        private void RecordView_Loaded(object sender, RoutedEventArgs e)
        {
            DateTimePickerStart.Value = DateTime.Today;
            DateTimePickerEnd.Value = DateTime.Now;
        }

        private static RecordView _view;
        private void RecordView_Closed(object sender, EventArgs e)
        {
            _view = null;
        }

        public static RecordView GetView()
        {
            if (_view == null)
            {
                _view = new RecordView();
            }

            return _view;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var DB = Servers.DB;
            DateTime dtStart;
            DateTime dtEnd;
            if (DateTimePickerStart.Value.HasValue)
            {
                dtStart = DateTimePickerStart.Value.Value;
            }
            else
            {
                MessageBox.Show("请选择正确的开始时间");
                return;
            }

            if (DateTimePickerEnd.Value.HasValue)
            {
                dtEnd = DateTimePickerEnd.Value.Value;
            }
            else
            {
                MessageBox.Show("请选择正确的结束时间");
                return;
            }

            try
            {
                var rlts = DB.Select<RecordResult>().Where(p => p.WorkingTime < dtEnd && p.WorkingTime > dtStart).ToList();
                DataGrid.ItemsSource = null;
                DataGrid.ItemsSource = rlts;
            }
            catch (Exception exception)
            {
                Logger.WriteSysLog("查询明细数据失败", exception);
                MessageBox.Show("查询明细数据失败，请检查");
            }

        }
    }
}
